﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string CardIN_D_Add(CardIN_D model ,int ACID);


[OperationContract]
string CardIN_D_Update(CardIN_D model ,int ACID);


[OperationContract]
string CardIN_DBydid_Delete(int did,int ACID);


[OperationContract]
CardIN_D CardIN_DBydid_Get(int did,int ACID );


[OperationContract]
List<CardIN_D> CardIN_D_Get(string sortBy,int ACID);




}
    public partial class Service : IService
    {
        #region  添加CardIN_D表数据
        /// <summary>
        /// 添加CardIN_D
        /// </summary>
        /// <param name="model">CardIN_D实体</param>
        /// <returns>空或错误</returns>
        public string CardIN_D_Add(CardIN_D model, int ACID)
        {
            if (model.did == null)
            {
                return "『详单流水号』不能为空！";
            }
            if (model.yue == null)
            {
                return "『卡余额』不能为空！";
            }
            if (model.amount == null)
            {
                return "『价格』不能为空！";
            }
            if (model.quantity == null)
            {
                return "『数量』不能为空！";
            }
            if (string.IsNullOrEmpty(model.unit))
            {
                return "『单位』不能为空！";
            }
            if (model.price == null)
            {
                return "『单价』不能为空！";
            }
            if (model.monthflag == null)
            {
                return "『按月核算标志』不能为空！";
            }
            if (model.ComboID == null)
            {
                return "『套餐ID』不能为空！";
            }
            if (string.IsNullOrEmpty(model.phoneno))
            {
                return "『手机号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.CardIN_D.Count(p => p.did == model.did) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.CardIN_D.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加CardIN_D表数据事物用重构
        /// <summary>
        /// 添加CardIN_D事物用重构
        /// </summary>
        /// <param name="model">CardIN_D实体</param>
        /// <returns>空或错误</returns>
        private string CardIN_D_Add(CardIN_D model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (model.did == null)
            {
                return "『详单流水号』不能为空！";
            }
            if (model.yue == null)
            {
                return "『卡余额』不能为空！";
            }
            if (model.amount == null)
            {
                return "『价格』不能为空！";
            }
            if (model.quantity == null)
            {
                return "『数量』不能为空！";
            }
            if (string.IsNullOrEmpty(model.unit))
            {
                return "『单位』不能为空！";
            }
            if (model.price == null)
            {
                return "『单价』不能为空！";
            }
            if (model.monthflag == null)
            {
                return "『按月核算标志』不能为空！";
            }
            if (model.ComboID == null)
            {
                return "『套餐ID』不能为空！";
            }
            if (string.IsNullOrEmpty(model.phoneno))
            {
                return "『手机号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            if (ctx.CardIN_D.Count(p => p.did == model.did) > 0)
            {
                return "主键已存在！";
            }
            ctx.CardIN_D.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion

        #region  修改CardIN_D表数据
        /// <summary>
        /// 修改CardIN_D
        /// </summary>
        /// <param name="model">CardIN_D实体</param>
        /// <returns>空或错误</returns>
        public string CardIN_D_Update(CardIN_D model, int ACID)
        {
            if (model.did == null)
            {
                return "『详单流水号』不能为空！";
            }
            if (model.yue == null)
            {
                return "『卡余额』不能为空！";
            }
            if (model.amount == null)
            {
                return "『价格』不能为空！";
            }
            if (model.quantity == null)
            {
                return "『数量』不能为空！";
            }
            if (string.IsNullOrEmpty(model.unit))
            {
                return "『单位』不能为空！";
            }
            if (model.price == null)
            {
                return "『单价』不能为空！";
            }
            if (model.monthflag == null)
            {
                return "『按月核算标志』不能为空！";
            }
            if (model.ComboID == null)
            {
                return "『套餐ID』不能为空！";
            }
            if (string.IsNullOrEmpty(model.phoneno))
            {
                return "『手机号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.CardIN_D.Count(p => p.did == model.did) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    CardIN_D CardIN_Dmodel = ctx.CardIN_D.Single(p => p.did == model.did);
                    if (model.did != null)
                    {
                        CardIN_Dmodel.did = model.did;
                    }
                    if (!string.IsNullOrEmpty(model.pid))
                    {
                        CardIN_Dmodel.pid = model.pid;
                    }
                    if (!string.IsNullOrEmpty(model.cno))
                    {
                        CardIN_Dmodel.cno = model.cno;
                    }
                    if (!string.IsNullOrEmpty(model.phoneno))
                    {
                        CardIN_Dmodel.phoneno = model.phoneno;
                    }
                    if (model.ComboID != null)
                    {
                        CardIN_Dmodel.ComboID = model.ComboID;
                    }
                    if (model.monthflag != null)
                    {
                        CardIN_Dmodel.monthflag = model.monthflag;
                    }
                    if (model.price != null)
                    {
                        CardIN_Dmodel.price = model.price;
                    }
                    if (!string.IsNullOrEmpty(model.unit))
                    {
                        CardIN_Dmodel.unit = model.unit;
                    }
                    if (model.quantity != null)
                    {
                        CardIN_Dmodel.quantity = model.quantity;
                    }
                    if (model.amount != null)
                    {
                        CardIN_Dmodel.amount = model.amount;
                    }
                    if (model.yue != null)
                    {
                        CardIN_Dmodel.yue = model.yue;
                    }
                    if (!string.IsNullOrEmpty(model.remarks))
                    {
                        CardIN_Dmodel.remarks = model.remarks;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除CardIN_D表Bydid数据
        /// <summary>
        /// 删除CardIN_DBydid
        /// </summary>
        /// <param name="did">详单流水号</param>
        /// <returns>空或错误</returns>
        public string CardIN_DBydid_Delete(int did, int ACID)
        {
            if (did == null)
            {
                return "『详单流水号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.CardIN_D.Count(p => p.did == did) != 1)
                    {
                        return null;
                    }
                    CardIN_D model = ctx.CardIN_D.Single(p => p.did == did);
                    ctx.CardIN_D.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除CardIN_D表Bydid数据[事物用重构]
        /// <summary>
        /// 删除CardIN_DBydid[事物用重构]
        /// </summary>
        /// <param name="did">详单流水号</param>
        /// <returns>空或错误</returns>
        private string CardIN_DBydid_Delete(int did, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (did == null)
            {
                return "『详单流水号』不能为空！";
            }
            if (ctx.CardIN_D.Count(p => p.did == did) != 1)
            {
                return null;
            }
            CardIN_D model = ctx.CardIN_D.Single(p => p.did == did);
            ctx.CardIN_D.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion

        #region  获取CardIN_D表Bydid数据
        /// <summary>
        /// 获取CardIN_DBydid
        /// </summary>
        /// <param name="did">详单流水号</param>
        /// <returns>当前表</returns>
        public CardIN_D CardIN_DBydid_Get(int did, int ACID)
        {
            if (did == null)
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.CardIN_D.Count(p => p.did == did) != 1)
                    {
                        return null;
                    }
                    return ctx.CardIN_D.SingleOrDefault(p => p.did == did);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion

        #region  获取CardIN_D表数据
        /// <summary>
        /// 获取CardIN_D
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<CardIN_D></returns>
        public List<CardIN_D> CardIN_D_Get(Expression<Func<CardIN_D, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.CardIN_D.ToList();
                        }
                        else
                        {
                            return ctx.CardIN_D.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.CardIN_D.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.CardIN_D.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion

        #region  获取CardIN_D表数据
        /// <summary>
        /// 获取CardIN_D
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<CardIN_D></returns>
        public List<CardIN_D> CardIN_D_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return CardIN_D_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion

        #region  获取CardIN_D表数据计数
        /// <summary>
        /// 获取CardIN_D表数据计数
        /// </summary>
        /// <returns>List<CardIN_D></returns>
        public int CardIN_D_GetCount(Expression<Func<CardIN_D, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.CardIN_D.Count();

                    }
                    else
                    {
                        return ctx.CardIN_D.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion

        #region  获取CardIN_D分页表数据
        /// <summary>
        /// 获取CardIN_D分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<CardIN_D></returns>
        public List<CardIN_D> CardIN_D_Getpage(int pageIndex, int pageSize, Expression<Func<CardIN_D, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.CardIN_D.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.CardIN_D.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.CardIN_D.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.CardIN_D.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion

        #region  批量修改CardIN_D表数据
        /// <summary>
        /// 批量修改CardIN_D
        /// </summary>
        /// <param name="predicate">Expression<Func<CardIN_D, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<CardIN_D,CardIN_D>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int CardIN_D_batchUpdate(Expression<Func<CardIN_D, bool>> predicate, Expression<Func<CardIN_D, CardIN_D>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.CardIN_D.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion

        #region  批量修改CardIN_D表数据事物用重构
        /// <summary>
        /// 批量修改CardIN_D事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<CardIN_D, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<CardIN_D,CardIN_D>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int CardIN_D_batchUpdate(Expression<Func<CardIN_D, bool>> predicate, Expression<Func<CardIN_D, CardIN_D>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.CardIN_D.Update(predicate, updater);
            return ok;
        }
        #endregion

        #region  批量删除CardIN_D表数据
        /// <summary>
        /// 批量删除CardIN_D
        /// </summary>
        /// <param name="predicate">Expression<Func<CardIN_D, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int CardIN_D_batchDel(Expression<Func<CardIN_D, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.CardIN_D.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion

        #region  批量删除CardIN_D表数据事物用重构
        /// <summary>
        /// 批量删除CardIN_D事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<CardIN_D, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int CardIN_D_batchDel(Expression<Func<CardIN_D, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.CardIN_D.Delete(predicate);
            return ok;
        }
        #endregion

    }
}

